<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 17-1-16
 * Time: 下午5:31
 */

//Float 浮点型
//浮点数的形式表示：
//LNUM          [0-9]+
//DNUM          ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
//EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})

//所以永远不要相信浮点数结果精确到了最后一位，也永远不要比较两个浮点数是否相等。

$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;
echo ($a-$b).PHP_EOL;
if(abs($a-$b) < $epsilon) {
    echo "true";
}
var_dump(PHP_EOL);
//NaN
//由于 NAN 代表着任何不同值，不应拿 NAN 去和其它值进行比较，包括其自身
$n = NAN;
var_dump($n);
var_dump($n == NAN);//false
var_dump(is_nan($n));//true

$x = 8 - 6.4;  // which is equal to 1.6
$y = 1.6;
var_dump($x == $y); // is not true
//PHP thinks that 1.6 (coming from a difference) is not equal to 1.6. To make it work, use round()
var_dump(round($x, 2) == round($y, 2)); // this is true

$float64 = pack("d", 5300231);
$binarydata64 =unpack('H*',$float64); //"000000C001385441"
var_dump($binarydata64);

$binarydata64 = pack('H*','000000C001385441');
$float64 = unpack("d", $binarydata64); // 5300231
var_dump($float64);


function str2num($str)
{
    if (strpos($str, '.') !== FALSE
        && strpos($str,    ',') !== FALSE
        && strpos($str, '.') < strpos($str,','))
    {
        $str = str_replace('.','',$str);
        $str = strtr($str,',','.');
    }
    else
    {
        $str = str_replace(',','',$str);
    }
    return (float)$str;
}

var_dump(str2num("123,345,56.001"));
var_dump(str2num("123,345,56.001,234,543"));
//var_dump(str2num("123.345,56,001"));
